<?php

namespace app\admin\controller;

use app\helper\Helper;
use app\model\DepartModel;
use think\Request;
use think\Validate;

class Depart extends Base
{
    protected $model;

    public function __construct(DepartModel $departModel)
    {
        parent::__construct();
        $this->model = $departModel;
    }

    /**
     * 列表展示
     * @return \think\response\View
     */
    public function index()
    {
        return $this->fetch('depart/index');
    }

    /**
     * 列表接口
     */
    public function departList()
    {
        $param = $this->request->param();
        $rule = [
            'admin_name|管理员名称'=>'max:10',
        ];
        $validate = new Validate($rule);
        if(!$validate->check($param)){
            return json(['code'=>500,'msg'=>$validate->getError()]);
        }

        $page = isset($param['page']) ? intval($param['page']) : 1;
        $pageSize = isset($param['limit']) ? intval($param['limit']) : 10;

        $arr = ['like'=>'admin.admin_name','1'=>'admin.bank_id'];
        $where = $this->select_param($arr,$param);
        $list =  $this->model->getDepartListPage($page,$pageSize,$where,false);
        $count =  $this->model->getDepartListPage(0,0,$where,true);
        return json(['code'=>0,'msg'=>'','count'=>$count,'data'=>$list]);
    }

    /**
     * 添加 编辑页面
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function saveDepartInfo()
    {
        $params = $this->request->param();
        if(!empty($params['id'])){
            $info = $this->model->where('id',$params['id'])->find();
            $this->assign('info',$info);
        }
        return $this->fetch('depart/saveDepartInfo');
    }

    public function saveInfo()
    {
        $params = $this->request->param();
        $rule = [
            'depart_name|部门名称'=>'require|max:10',
            'status|状态'=>'require|number',
        ];

        $validate = new Validate($rule);
        if(!$validate->check($params)){
            return json(['code'=>400,'msg'=>$validate->getError()]);
        }
        $data=[
            'depart_name'=>$params['depart_name'],
            'status'=>$params['status'],
        ];

        if(!empty($params['bank_id'])){
            $data['update_ts'] = date('Y-m-d H:i:s',time());
            $res =  $this->model->saveData($data,['id'=>$params['bank_id']]);
        }else{
            $data['created_ts'] = date('Y-m-d H:i:s',time());
            $res =  $this->model->saveData($data);
        }
        if($res !== false){
            return json(['code'=>200,'msg'=>'保存成功','data'=>$res]);
        }else{
            return json(['code'=>400,'msg'=>'保存失败','data'=>$res]);
        }

    }




    /**
     * 删除部门
     */
    public function departDel()
    {
        $param=$this->request->param();
        $rule = [
            'id'=>'require|number',
        ];
        $validate = new Validate($rule);
        if(!$validate->check($param)){
            return json(['code' => 500,'msg'=>$validate->getError()]);
        }
        $res =  $this->model->where('id',$param['id'])->update(['deleted'=>time()]);
        if($res){
            return json(['code' => 200,'msg'=>'删除成功']);
        }else{
            return json(['code' => 500,'msg'=>'删除失败']);
        }
    }
}